home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / database / iex.c < prev    next >
Text File  |  1984-05-21  |  7KB  |  278 lines

  1. /* SDB - import/export command routines */
  2.  
  3. #include "stdio.h"
  4. #include "sdbio.h"
  5.  
  6. extern int dbv_token;
  7. extern char dbv_tstring[];
  8. extern int dbv_tvalue;
  9.  
  10. /* db_import - import tuples from a file */
  11. int *db_import(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  12.   char *fmt;
  13. {
  14.     struct scan *sptr;
  15.     struct attribute *aptr;
  16.     char fname[STRINGMAX+1],avalue[STRINGMAX+1];
  17.     int tcnt,astart,i,eofile;
  18.     FILE *fp;
  19.  
  20.     /* check for a command line */
  21.     if (fmt != NULL)
  22.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  23.  
  24.     /* checks for "<filename> into <relation-name>" */
  25.     if (db_ntoken() == ID)
  26.         strcat(dbv_tstring,".dat");
  27.     else if (dbv_token != STRING)
  28.         return (db_ferror(SYNTAX));
  29.     strcpy(fname,dbv_tstring);
  30.     if (db_ntoken() != INTO)
  31.         return (db_ferror(SYNTAX));
  32.     if (db_ntoken() != ID)
  33.         return (db_ferror(SYNTAX));
  34.  
  35.     /* open the relation */
  36.     if ((sptr = db_ropen(dbv_tstring)) == NULL)
  37.         return (FALSE);
  38.  
  39.     /* open the input file */
  40.     if ((fp = fopen(fname,"r")) == NULL)
  41.         return (db_ferror(INPFNF));
  42.  
  43.     /* import tuples */
  44.     eofile = FALSE;
  45.     for (tcnt = 0; ; tcnt++) {
  46.  
  47.         /* get attribute values */
  48.         astart = 1;
  49.         for (i = 0; i < NATTRS; i++) {
  50.  
  51.             /* get a pointer to the current attribute */
  52.             aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  53.  
  54.             /* check for the last attribute */
  55.             if (aptr->at_name[0] == 0)
  56.                 break;
  57.  
  58.             /* input the tuple */
  59.             if (fgets(avalue,STRINGMAX,fp) == 0) {
  60.                 eofile = TRUE;
  61.                 break;
  62.             }
  63.             avalue[strlen(avalue)-1] = EOS;
  64.  
  65.             /* store the attribute value */
  66.             db_aput(aptr,&sptr->sc_tuple[astart],avalue);
  67.  
  68.             /* update the attribute start */
  69.             astart += aptr->at_size;
  70.         }
  71.  
  72.         /* store the new tuple */
  73.         if (!eofile) {
  74.             if (!db_rstore(sptr)) {
  75.                 db_rclose(sptr);
  76.                 return (FALSE);
  77.             }
  78.         }
  79.         else
  80.             break;
  81.     }
  82.  
  83.     /* close the relation */
  84.     db_rclose(sptr);
  85.  
  86.     /* close the input file */
  87.     fclose(fp);
  88.  
  89.     /* check number of tuples imported */
  90.     if (tcnt != 0) {
  91.  
  92.         /* print tuple count */
  93.         printf("[ %d imported ]\n",tcnt);
  94.     }
  95.     else
  96.         printf("[ none imported ]\n");
  97.  
  98.     /* return successfully */
  99.     return (TRUE);
  100. }
  101.  
  102. /* db_export - export tuples to a file */
  103. int *db_export(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  104.   char *fmt;
  105. {
  106.     struct scan *sptr;
  107.     struct attribute *aptr;
  108.     char rname[STRINGMAX+1],avalue[STRINGMAX+1];
  109.     int tcnt,astart,i;
  110.     FILE *fp;
  111.  
  112.     /* check for a command line */
  113.     if (fmt != NULL)
  114.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  115.  
  116.     /* checks for "<relation-name> [ into <filename> ]" */
  117.     if (db_ntoken() != ID)
  118.         return (db_ferror(SYNTAX));
  119.     strcpy(rname,dbv_tstring);
  120.     if (!db_to(&fp,".dat"))
  121.         return (FALSE);
  122.  
  123.     /* open the relation */
  124.     if ((sptr = db_ropen(rname)) == NULL)
  125.         return (FALSE);
  126.  
  127.     /* export tuples */
  128.     for (tcnt = 0; db_rfetch(sptr); tcnt++) {
  129.  
  130.         /* get attribute values */
  131.         astart = 1;
  132.         for (i = 0; i < NATTRS; i++) {
  133.  
  134.             /* get a pointer to the current attribute */
  135.             aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  136.  
  137.             /* check for the last attribute */
  138.             if (aptr->at_name[0] == 0)
  139.                 break;
  140.  
  141.             /* get the attribute value */
  142.             db_aget(aptr,&sptr->sc_tuple[astart],avalue);
  143.  
  144.             /* output the tuple */
  145.             fprintf(fp,"%s\n",avalue);
  146.  
  147.             /* update the attribute start */
  148.             astart += aptr->at_size;
  149.         }
  150.     }
  151.  
  152.     /* close the relation */
  153.     db_rclose(sptr);
  154.  
  155.     /* close the output file */
  156.     if (fp != stdout)
  157.         fclose(fp);
  158.  
  159.     /* check number of tuples exported */
  160.     if (tcnt != 0) {
  161.  
  162.         /* print tuple count */
  163.         printf("[ %d exported ]\n",tcnt);
  164.     }
  165.     else
  166.         printf("[ none exported ]\n");
  167.  
  168.     /* return successfully */
  169.     return (TRUE);
  170. }
  171.  
  172. /* db_squeeze - squeeze deleted tuples from a relation file */
  173. int *db_squeeze(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  174.   char *fmt;
  175. {
  176.     struct scan *sptr;
  177.  
  178.     /* check for a command line */
  179.     if (fmt != NULL)
  180.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  181.  
  182.     /* checks for "<relation-name>" */
  183.     if (db_ntoken() != ID)
  184.         return (db_ferror(SYNTAX));
  185.  
  186.     /* open the relation */
  187.     if ((sptr = db_ropen(dbv_tstring)) == NULL)
  188.         return (FALSE);
  189.  
  190.     /* compress the relation file */
  191.     if (!db_rcompress(sptr)) {
  192.         db_rclose(sptr);
  193.         return (FALSE);
  194.     }
  195.  
  196.     /* close the relation */
  197.     db_rclose(sptr);
  198.  
  199.     /* return successfully */
  200.     return (TRUE);
  201. }
  202.  
  203. /* db_extract - extract a relation definition */
  204. int *db_extract(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  205.   char *fmt;
  206. {
  207.     struct scan *sptr;
  208.     struct attribute *aptr;
  209.     char rname[STRINGMAX+1],aname[ANSIZE+1],*atype;
  210.     int i;
  211.     FILE *fp;
  212.  
  213.     /* check for a command line */
  214.     if (fmt != NULL)
  215.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  216.  
  217.     /* checks for "<relation-name> [ into <filename> ]" */
  218.     if (db_ntoken() != ID)
  219.         return (db_ferror(SYNTAX));
  220.     strcpy(rname,dbv_tstring);
  221.     if (!db_to(&fp,".def"))
  222.         return (FALSE);
  223.  
  224.     /* open the relation */
  225.     if ((sptr = db_ropen(rname)) == NULL)
  226.         return (FALSE);
  227.  
  228.     /* output the relation definition */
  229.     fprintf(fp,"create %s (\n",rname);
  230.  
  231.     /* get attribute values */
  232.     for (i = 0; i < NATTRS; i++) {
  233.  
  234.         /* get a pointer to the current attribute */
  235.         aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  236.  
  237.         /* check for the last attribute */
  238.         if (aptr->at_name[0] == 0)
  239.             break;
  240.  
  241.         /* get the attribute name */
  242.         stccpy(aname,aptr->at_name,ANSIZE); aname[ANSIZE] = 0;
  243.  
  244.         /* determine the attribute type */
  245.         switch (aptr->at_type) {
  246.         case TCHAR:
  247.                 atype = "char";
  248.                 break;
  249.         case TNUM:
  250.                 atype = "num";
  251.                 break;
  252.         default:
  253.                 atype = "<error>";
  254.                 break;
  255.         }
  256.  
  257.         /* output the attribute definition */
  258.         if (strlen(aname) < 8)
  259.             fprintf(fp,"\t%s\t\t%s\t%d\n",aname,atype,aptr->at_size);
  260.         else
  261.             fprintf(fp,"\t%s\t%s\t%d\n",aname,atype,aptr->at_size);
  262.     }
  263.  
  264.     /* output the relation size */
  265.     fprintf(fp,") %d\n",sptr->sc_relation->rl_tmax);
  266.  
  267.     /* close the relation */
  268.     db_rclose(sptr);
  269.  
  270.     /* close the output file */
  271.     if (fp != stdout)
  272.         fclose(fp);
  273.  
  274.     /* return successfully */
  275.     return (TRUE);
  276. }
  277.  
  278.